/**
 * 
 */
package org.mips.Sirius.provider.SIRI;

import java.io.StringWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.mips.Sirius.components.StopPoint;
import org.mips.Sirius.provider.ProviderConnection;
import org.mips.Sirius.provider.ProviderException;
import org.xmlpull.v1.XmlSerializer;

import android.util.Log;
import android.util.Xml;

/**
 * 
 * @author Alessandro
 * 
 */
public class SIRIStopMonitoringRequest {

	String requestorRef;
	String stopMonitoringRequest;
	StopPoint specificStopPoint;

	public SIRIStopMonitoringRequest(String requestorRef)
			throws ProviderException {
		this.requestorRef = requestorRef;

		createRequest();
	}

	public SIRIStopMonitoringRequest(String requestorRef,
			StopPoint specificStopPoint) throws ProviderException {
		this.requestorRef = requestorRef;
		this.specificStopPoint = specificStopPoint;

		createRequest();
	}

	private void createRequest() throws ProviderException {

		stopMonitoringRequest = "";

		XmlSerializer serializer = Xml.newSerializer();
		StringWriter writer = new StringWriter();

		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssZ");
		Date date = new Date();
		String currentTimestamp = dateFormat.format(date);

		try {
			serializer.setOutput(writer);
			serializer.startDocument("UTF-8", true);

			serializer.startTag("", "Siri");
			serializer.attribute("", "xmlns", "http://www.siri.org.uk/");
			serializer.attribute("", "xmlns:xsi",
					"http://www.w3.org/2001/XMLSchema-instance");
			serializer.attribute("", "xsi:schemaLocation",
					"http://www.siri.org.uk/..\\siri.xsd");
			serializer.attribute("", "version", "1.0");

			serializer.startTag("", "ServiceRequest");

			serializer.startTag("", "RequestTimestamp");
			serializer.text(currentTimestamp);
			serializer.endTag("", "RequestTimestamp");

			serializer.startTag("", "RequestorRef");
			serializer.text(requestorRef);
			serializer.endTag("", "RequestorRef");

			serializer.startTag("", "StopMonitoringRequest");
			serializer.attribute("", "version", "1.0");

			serializer.startTag("", "RequestTimestamp");
			serializer.text(currentTimestamp);
			serializer.endTag("", "RequestTimestamp");

			if (this.specificStopPoint != null) {
				serializer.startTag("", "MonitoringRef");
				serializer.text(this.specificStopPoint.getReference());
				serializer.endTag("", "MonitoringRef");
			}

			serializer.startTag("", "StopMonitoringDetailLevel");
			serializer.text("calls");
			serializer.endTag("", "StopMonitoringDetailLevel");

			serializer.endTag("", "StopMonitoringRequest");

			serializer.endTag("", "ServiceRequest");

			serializer.endTag("", "Siri");

			serializer.endDocument();

			stopMonitoringRequest = writer.toString();
		} catch (Exception e) {
			Log.d("tap", "Couldn't create a StopMonitoringRequest: "
					+ e.getMessage());
			throw new ProviderException(e,
					"Couldn't create StopMonitoringRequest");
		}

		Log.d("tap", "StopMonitoringRequest created: " + stopMonitoringRequest);
	}

	public SIRIStopMonitoringDelivery send(ProviderConnection connection)
			throws ProviderException {

		String stopMonitoringDelivery = connection.send(stopMonitoringRequest);

		Log.d("tap", "StopMonitoringRequest received: "
				+ stopMonitoringDelivery);

		return new SIRIStopMonitoringDelivery(stopMonitoringDelivery);
	}

}
